var app = getApp();
var utils = require('../../utils/util.js');
Page({
  data: {
    //头像
    pictrue: '',
    //性别
    sex: 1,
    //名称
    name: '',
    //品种
    breed: '',
    //年
    yearRange: [],
    yearValue: 0,
    //月
    monthRange: [],
    monthValue: 0,
    //日
    dayRange: [],
    dayValue: 0,
    //是否点击过
    isSubmit: false,
    //主食
    staple: '',
    //零食
    snacks: '',
    pet: null

  },
  onLoad: function(options) {
    var pet = JSON.parse(options.pet)
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15
    })
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var monthRange = [];
    for (var i = 1; i <= 12; i++) {
      monthRange.push(i);
    }
    var day = date.getDate();
    var yearRange = [];
    for (var i = 0; i < 15; i++) {
      yearRange.push(year - i);
    }
    var dayRange = [];
    switch (month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        for (var i = 1; i <= 31; i++) {
          dayRange.push(i);
        }
        break;
      case 2:
      case 4:
      case 6:
      case 9:
      case 11:
        for (var i = 1; i <= 30; i++) {
          dayRange.push(i);
        }
        break;
    }
    var birthday = pet.birthday;

    var fullYear1 = parseInt(birthday.substring(0, 4));
    for (var i = 0; i < yearRange.length; i++) {
      if (fullYear1 == yearRange[i]) {
        this.setData({
          yearValue: i
        })
        break;
      }
    }
    var month1 = parseInt(birthday.substring(5, 7));
    for (var i = 0; i < monthRange.length; i++) {
      if (month1 == monthRange[i]) {
        this.setData({
          monthValue: i
        })
        break;
      }
    }
    var day1 = parseInt(birthday.substring(8, 10));
    for (var i = 0; i < dayRange.length; i++) {
      if (day1 == dayRange[i]) {
        this.setData({
          dayValue: i
        })
        break;
      }
    }
    this.setData({
      dayRange: dayRange,
      yearRange: yearRange,
      monthRange: monthRange,
      pictrue: pet.pictrue,
      sex: pet.gender,
      name: pet.name,
      breed: pet.breed,
      staple: pet.staple,
      snacks: pet.snacks,
      pet: pet
    })
  },

  //选择男
  selMan: function() {
    var sex = this.data.sex;
    if (sex == 1) {
      return;
    } else {
      this.setData({
        sex: 1
      })
    }
  },
  //选择女
  selWoMan: function() {
    var sex = this.data.sex;
    if (sex == 0) {
      return;
    } else {
      this.setData({
        sex: 0
      })
    }
  },
  //选择年
  seleYear: function(e) {
    var index = e.detail.value;
    this.setData({
      yearValue: index,

    })
  },
  //选择年
  seleMonth: function(e) {
    var index = e.detail.value;
    this.setData({
      monthValue: index,

    })
  },
  //选择年
  seleDay: function(e) {
    var index = e.detail.value;
    this.setData({
      dayValue: index,

    })
  },
  //添加图片
  addPic: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function(r) {
        that.setData({
          pictrue: r.tempFiles[0].path
        })
      },
    })
  },
  //输入名称
  inputName: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过5
    if (cursor > 5) {
      this.setData({
        name: value.substring(0, 5)
      })
    } else {
      this.setData({
        name: value
      })
    }
  },

  //输入主食
  inputStaple: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过20
    if (cursor > 20) {
      this.setData({
        staple: value.substring(0, 20)
      })
    } else {
      this.setData({
        staple: value
      })
    }
  },
  //输入零食
  inputSnacks: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过20
    if (cursor > 20) {
      this.setData({
        snacks: value.substring(0, 20)
      })
    } else {
      this.setData({
        snacks: value
      })
    }
  },
  //输入品种
  inputBreed: function(e) {
    //当前输入数量
    var cursor = e.detail.cursor;
    var value = e.detail.value;
    //不能超过5
    if (cursor > 10) {
      this.setData({
        breed: value.substring(0, 10)
      })
    } else {
      this.setData({
        breed: value
      })
    }
  },
  //返回
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },

  //更改头像
  updatePictrue: function(name, gender, breed, birthday, staple, snacks) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getUploadFileOssPolicy',
      method: 'GET',
      data: {
        userId: app.globalData.user.id,
        type: 1
      },
      success: function(r) {
        if (r.data.code == 200) {
          var s = that.data.pet.pictrue;
          var src = s.substring(45, s.length);
          var host = r.data.data.host
          wx.uploadFile({
            url: host,
            filePath: that.data.pictrue,
            name: 'file',
            formData: {
              key: src,
              policy: r.data.data.policy,
              OSSAccessKeyId: r.data.data.accessid,
              success_action_status: '200',
              signature: r.data.data.signature
            },
            success: function() {
              that.updatePetReq(name, gender, breed, birthday, staple, snacks);
            },
            fail: function() {
              wx.showToast({
                title: '保存爱宠信息失败',
                icon: 'none',
                duration: 3000
              })
            }
          })
        } else {
          wx.showToast({
            title: '保存爱宠信息失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '保存爱宠信息失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },

  //更改
  updatePet: function() {
    var pictrue = this.data.pictrue;

    var name = this.data.name;
    var breed = this.data.breed;
    if (name == '') {
      wx.showToast({
        title: '请输入宠物名字',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (breed == '') {
      wx.showToast({
        title: '请输入宠物品类',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    var yearValue = this.data.yearRange[this.data.yearValue];
    var monthValue = this.data.monthRange[this.data.monthValue];
    if (monthValue < 10) {
      monthValue = '0' + monthValue;
    }
    var dayValue = this.data.dayRange[this.data.dayValue];
    if (dayValue < 10) {
      dayValue = '0' + dayValue;
    }
    var birthday = yearValue + '-' + monthValue + '-' + dayValue + ' ' + '00:00:00.000';
    var date = new Date();
    var year1 = date.getFullYear();
    var month1 = date.getMonth() + 1;
    var day1 = date.getDate();
    if (yearValue == year1) {
      if (monthValue > month1) {
        wx.showToast({
          title: '请选择不大于今天的日期',
          icon: 'none',
          duration: 3000
        })
        return;
      }
      if (dayValue > day1) {
        wx.showToast({
          title: '请选择不大于今天的日期',
          icon: 'none',
          duration: 3000
        })
        return;
      }
    }
    var gender = this.data.sex;
    var staple = this.data.staple;
    var snacks = this.data.snacks;
    var that = this;

    //如果点击过
    if (this.data.isSubmit) {
      return;
    }
    this.data.isSubmit = true;
    if (pictrue != this.data.pet.pictrue) {
      this.updatePictrue(name, gender, breed, birthday, staple, snacks);
    } else {
      this.updatePetReq(name, gender, breed, birthday, staple, snacks);
    }

  },
  updatePetReq: function(name, gender, breed, birthday, staple, snacks) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + '/updatePet',
      method: 'POST',
      data: {
        belongUserId:app.globalData.user.id,
        id: that.data.pet.id,
        name: name,
        gender: gender,
        breed: breed,
        birthday: birthday,
        staple: staple,
        snacks: snacks
      },
      success: function(r) {
        if (r.data.code == 200) {
          wx.showToast({
            title: '保存爱宠成功',
            duration: 2000
          })
          var timeOut = setTimeout(function() {
            clearTimeout(timeOut);
            wx.navigateBack({
              delta: 1
            })
          }, 3000)
          that.getAllPetByUserId();
        } else {
          wx.showToast({
            title: '保存爱宠失败,请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '保存爱宠失败,请稍后重试',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  //获取宠物列表
  getAllPetByUserId: function() {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'getAllPetByUserId',
      data: {
        userId: app.globalData.user.id
      },
      success: function(r) {
        if (r.data.code == 200) {
          var petList = r.data.data;
          app.globalData.petList = null;
          app.globalData.petList = petList;
        } else {
          wx.showToast({
            title: '获取宠物资料失败,请稍后重试',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,获取宠物资料失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
})